# -*- coding:utf-8 -*-
# @Time: 2020/9/8 9:57
# @Author: gupeng
# @File: hash_test.py

from redis import Redis
from hash.shorty_url import ShortyUrl
from hash.counter import Counter
from hash.login_session import LoginSession
from hash.graph import Graph
from hash.article import Article

client = Redis()


def shorty_url_test():
    shorty_url = ShortyUrl(client)
    short_id = shorty_url.shorten('www.baidu.com')
    print('short_id = ' + str(short_id))
    print('shorty_url = ' + str(shorty_url.restore(short_id)))


def counter_test():
    counter = Counter(client, 'hash_counter', 'first_counter')
    print('increase 10 = ' + str(counter.increase(10)))
    print('decrease 1 = ' + str(counter.decrease()))
    print('current counter = ' + str(counter.get()))
    counter.reset()
    print('counter after reset = ' + str(counter.get()))


def login_session_test():
    login_session = LoginSession(client, '1')
    user_token = login_session.create(20)
    print('user_token = ' + str(user_token))
    print('valid session result = ' + str(login_session.validate(user_token)))
    login_session.destroy()
    print('valid session result = ' + str(login_session.validate(user_token)))


def graph_test():
    graph = Graph(client, 'graph')
    graph.add_edge('a', 'b', 10)
    graph.add_edge('c', 'd', 20)
    graph.add_multi_edges(('b', 'd', 30), ('d', 'e', 40))
    print('a->b = ' + str(graph.get_edge_weight('a', 'b')))
    print('a->b is exist ' + str(graph.has_edge('a', 'b')))
    print('all edges = ' + str(graph.get_all_edge()))
    print('all edges with weights = ' + str(graph.get_all_edge_with_weight()))


def article_test():
    article = Article(client, 'article_hash')
    print('create ' + str(article.create('title1', 'content1', 'author1')))
    print('update ' + str(article.update('title1-u', 'content1-u', 'author1-u')))
    print('get ' + str(article.get()))
    print('is_exists ' + str(article.is_exists()))


if __name__ == '__main__':
    # shorty_url_test()
    # counter_test()
    # login_session_test()
    # graph_test()
    article_test()
